<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <cost-plan v-bind="modalProps" v-model="model.tenantIds" />
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
  import { getAction, postFormAction } from '@/api/manage'
  import costPlan from '@/views/logistics/sales/modules/costPlan'
  export default {
    name: 'LogisticsAssignCustomersForm',
    mixins: [],
    components: {
      costPlan
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 7 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {},
        url: {
          assignMerchant: "/logistics/product/logisticsProduct/assignTenant",
          list: "/logistics/productMerchant/logisticsProductMerchant/list",
        },
        modalProps: {
          modalPropsCustom: true, //自定义，区分已有业务
          columns: [
            {
              align: "center",
              dataIndex: "departName",
              title: "客户名称",
              width: "25%",
              isShow: true //是否在已选中中显示
            },
            {
              align: "center",
              dataIndex: "orgCode",
              title: "客户ID",
              width: "25%",
              isShow: true //是否在已选中中显示
            },
            {
              align: "center",
              dataIndex: "departLevel_dictText",
              title: "客户级别",
              width: "25%",
              isShow: true //是否在已选中中显示
            },
            {
              align: "center",
              dataIndex: "groupId_dictText",
              title: "客户分组",
              width: "25%",
            }
          ],
          listUrl: "/sys/sysDepart/list",
          multiple: true,
          queryParamConfig: {},
          queryConfig: [
            {key: 'departName', label: '客户名称'},
            {key: 'departLevel', label: '客户级别', dictCode: 'merchant_level'},
            {key: 'groupId', label: '客户分组', dictCode: 'hz_sys_merchant_group,name,id,system'},
          ]
        },
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
      //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        if(this.model.code){
          this.initIenantIds();
        }
      },
      initIenantIds(){
        let params= { productCode: this.model.code,pageNo: 1, pageSize: 9999 };
        getAction(this.url.list, params).then((res) => {
          if(res.success){
            let records = (res.result && res.result.records) || [];
            this.$set(this.model, 'tenantIds', records.map(item => item.departId).join(','));
          }else{
            this.$message.warning(res.message);
          }
        })
      },
      handleOk_(){
        if(!this.model.tenantIds){
          this.$message.warning('请选择客户!');
          return;
        }
        let params = {
          tenantIds: this.model.tenantIds,
          productCode: this.model.code
        }
        postFormAction(this.url.assignMerchant, params).then((res) => {
          if(res.success){
            this.$message.success(res.message);
            this.$emit('ok');
          }else{
            this.$message.warning(res.message);
          }
        })
      },
    }
  }
</script>

<style scoped>
</style>